<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="page.query" inline label-position="right" label-width="90px">
        <el-form-item label="送货日期" prop="shipmentDateRange">
          <el-date-picker
            v-model="page.query.shipmentDateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <!--        <el-form-item label="产品名称" prop="productId">
          <product-query ref="productQuery" v-model="page.query.productId" @choose="handleChooseProduct" />
        </el-form-item>-->
        <el-form-item label="客户名称" prop="customerId">
          <customer-query ref="customerQuery" v-model="page.query.customerId" @choose="handleChooseCustomer" />
        </el-form-item>
        <el-form-item label="送货单号" prop="shipmentNo">
          <el-input v-model="page.query.shipmentNo" placeholder="请输入送货单号" clearable />
        </el-form-item>
        <el-form-item label="送货方式" prop="shipmentType">
          <dict-select ref="dictSelect1" v-model="page.query.shipmentType" type="button" dict-key="shipment_type" placeholder="请选择送货方式" @input="doPage" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <el-tabs v-model="page.query.shipmentType" @tab-click="handleTabClick">
        <el-tab-pane
          v-for="(item,index) in shipmentTypeList"
          :key="index"
          :label="item.stateName+'('+item.count+')'"
          :name="item.state"
        />
      </el-tabs>
      <div class="table-button">
        <el-button type="primary" class="el-icon-plus" @click="saveClick">新增</el-button>
        <el-button type="warning" class="el-icon-edit" plain @click="updateClick('tableBtn')">修改</el-button>
        <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
        <el-button type="primary" plain class="el-icon-printer" @click="handlePrint('tableBtn')">打印送货单</el-button>
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="送货单号" prop="shipmentNo" min-width="110">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="updateClick('column',scope.row)"
            >{{ scope.row.shipmentNo }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="送货日期" prop="shipmentDate" min-width="100" />
        <el-table-column label="客户名称" prop="customerName" min-width="100" />
        <el-table-column label="送货数量" prop="quantity" min-width="100" />
        <el-table-column label="总金额" prop="amount" min-width="100">
          <template slot-scope="scope">
            <money-format v-model="scope.row.amount" />
          </template>
        </el-table-column>
        <el-table-column label="收件人名称" prop="recipientName" min-width="100" />
        <el-table-column label="收件人手机" prop="recipientPhoneNum" min-width="100" />
        <el-table-column label="收件人地址" prop="recipientAddress" min-width="120" show-overflow-tooltip />
        <el-table-column label="备注" prop="remark" min-width="120" show-overflow-tooltip />
        <el-table-column label="操作" min-width="140" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handlePrint('column',scope.row)"
            >打印
            </el-button>
            <el-button
              type="text"
              @click="updateClick('column',scope.row)"
            >修改
            </el-button>
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              placement="top"
              style="margin-left: 10px"
              title="是否删除此条数据,删除此送货信息后，送货数量将回退至库存, 是否继续？"
              @confirm="handleDelConfirm(scope.row)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import { shipment_delete, shipment_page, shipment_update, shipment_type_count } from '@/api/sale/shipment'
import SubmitModule from '@/views/sale/shipment/modules/submitModule.vue'

export default {
  name: 'Index',
  components: { SubmitModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      shipmentTypeList: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          shipmentNo: '',
          shipmentDateRange: [],
          shipmentType: '',
          orderNo: ''
        }
      },
      dialogVisible: false
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      shipment_page(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
      this.selectShipmentTypeCount()
    },
    selectShipmentTypeCount() {
      this.tableLoading = true
      shipment_type_count(this.page.query).then(resp => {
        if (resp.success) {
          this.shipmentTypeList = resp.data
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.page.query.customerId = undefined
      this.page.query.productId = undefined
      this.$refs.customerQuery.resetQueryForm()
      this.$refs.dictSelect1.resetActive()
      this.page.query.shipmentType = '-1'
      this.doPage()
    },
    openOrClose(row) {
      this.tableLoading = true
      shipment_update(row).then(resp => {
        if (resp.success) {
          this.$message.success('操作成功')
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 新增按钮点击
    saveClick() {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init()
      })
    },
    // 修改按钮点击
    updateClick(type, row) {
      let id = ''
      if (type === 'column') {
        id = row.id
      } else {
        if (this.selected.length !== 1) {
          this.$message.warning('请选择一条需要修改的数据')
          return
        }
        id = this.selected[0].id
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(id)
      })
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据,是否继续?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        shipment_delete(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    },
    handlePrint(type, row) {
      let id
      if (type === 'tableBtn') {
        if (this.selected.length !== 1) {
          this.$message.warning('请选择一条需要打印的数据')
          return
        }
        id = this.selected[0].id
      } else {
        id = row.id
      }
      const routeData = this.$router.resolve({
        path: '/order/shipment/print',
        query: {
          id: id,
          'token': sessionStorage.getItem('authorization')
        }
      })
      window.open(routeData.href, '_blank')
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    // 表单关闭
    handleClose() {
      this.dialogVisible = false
      this.doPage()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      shipment_delete(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    handleChooseCustomer(row) {
      if (row) {
        this.page.query.customerId = row.id
      }
    },
    handleChooseProduct(row) {
      if (row) {
        this.page.query.productId = row.id
      }
    },
    handleTabClick(tab) {
      this.page.query.shipmentType = tab.name
      this.doPage()
    }
  }
}
</script>

<style scoped>

</style>
